import 'package:homepad/iot/basic_node.dart';
import 'package:homepad/iot/basic_types.dart';
import 'package:homepad/iot/message_def.dart';

class HumiditySensor extends BasicNode {
  HumiditySensor.fromJson(super.json) : super.fromJson();

  int _humidity = 60;

  int get humidity => _humidity;

  @override
  void readAllState() {
    readAttribute(ClusterIds.relativeHumidityMeasurement, MeasurementAttributeIds.measuredValue);
  }

  @override
  bool onAttributeReport(AttributePathIB path, data) {
    if (path.cluster != ClusterIds.relativeHumidityMeasurement) {
      return super.onAttributeReport(path, data);
    }

    if (path.attribute == MeasurementAttributeIds.measuredValue) {
      var newValue = data as int;
      if (newValue == _humidity) {
        return false;
      }
      _humidity = newValue;
      return true;
    }
    return false;
  }
}
